﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using System8ToyMC;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Automation.Provider;

namespace t_System8ToyMCControls
{
    [TestClass]
    public class t_MainPage : PresentationTest
    {
        MainPage _val;
        SaveLoad _saveLoad;
        LoadFromTextDialog _loader;
        System8Parameters _params;

        Button _ConvertDialog;

        [TestInitialize]
        public void TestInit()
        {
            _val = new MainPage();
            _saveLoad = _val.FindName("SaveLoad") as SaveLoad;
            _ConvertDialog = _val.FindName("ConvertFromText") as Button;
            _loader = _val.FindName("ConversionTextEntry") as LoadFromTextDialog;
            _params = _val.FindName("System8Params") as System8Parameters;

            TestPanel.Children.Add(_val);
        }

        [TestMethod]
        public void TestFoundControls()
        {
            Assert.IsNotNull(_saveLoad, "Save load was not found");
            Assert.IsNotNull(_ConvertDialog, "Could not find convert dialog button");
            Assert.IsNotNull(_loader, "Could not find the conversion dialog box!");
            Assert.IsNotNull(_params, "Could not find teh system 8 parameters!");
        }

        [TestMethod]
        public void TestSaveLoadHookedUp()
        {
            Assert.IsNotNull(_saveLoad.SavedNames, "The saved names should not be null");
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(2000)]
        public void TestLoadingData()
        {
            var text = "n: b=14814 c=11849 l=667\n p: b=7623 c=6083 l=349\n n1: b=6466 c=10001 l=602\n p1: b=3246 c=5131 l=314\n n2: b=11818 c=5418 l=472\n p2: b=6108 c=2848 l=243\n n12: b=5110 c=4534 l=421\n p12: b=2572 c=2390 l=216";

            ///
            /// Make the dialog box visible...
            /// 

            ButtonAutomationPeer bp = new ButtonAutomationPeer(_ConvertDialog);
            IInvokeProvider bi = (IInvokeProvider)bp;
            EnqueueCallback(() => bi.Invoke());
            EnqueueConditional(() => _loader.Opacity == 1.0);

            ///
            /// Now, enter the text
            /// 

            TextBox enterBox = _loader.FindName("TBConvertText") as TextBox;
            TextBoxAutomationPeer tbp = new TextBoxAutomationPeer(enterBox);
            IValueProvider ivp = (IValueProvider)tbp;
            EnqueueCallback(() => ivp.SetValue(text));

            ///
            /// Wait until the convert guy is now enabled...
            /// 

            Button cvt = _loader.FindName("BConvert") as Button;
            EnqueueConditional(() => cvt.IsEnabled);

            ///
            /// Ok, now push the button
            /// 

            ButtonAutomationPeer cvtAuto = new ButtonAutomationPeer(cvt);
            IInvokeProvider cvtPush = (IInvokeProvider)cvtAuto;
            EnqueueCallback(() => cvtPush.Invoke());

            ///
            /// Now, the conversion should happen
            /// 

            EnqueueConditional(() => _params.Equations.n.Bottom == 14814);
            EnqueueTestComplete();
        }
    }
}